package com.example.demo.zaqizabatest;

import lombok.SneakyThrows;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SuppressWarnings("AlibabaAvoidManuallyCreateThread")
public class WuliaoTest {

    @SuppressWarnings("AlibabaThreadPoolCreation")
    private static final ExecutorService executorService = Executors.newFixedThreadPool(4);


    static class PrintAB {
        private Object object = new Object();
        private boolean flag = true;

        public  void printA() throws InterruptedException {
            synchronized(object){
                for (int i = 0; i < 5; i++) {
                    object.notifyAll();
                    System.out.print("A");
                    if (i != 4){
                        object.wait();
                    }
                }
            }
        }

        public void printB() throws InterruptedException {
            synchronized(object){
                for (int i = 0; i < 5; i++) {
                    object.notifyAll();
                    System.out.print("B");
                    if (i != 4){
                        object.wait();
                    } else {
                        System.out.println();
                    }
                }
            }
        }

    }


    public static void main(String[] args) {

        final PrintAB print = new PrintAB();

        new Thread(new Runnable() {
            @Override
            @SneakyThrows
            public void run() {
                print.printA();
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            @SneakyThrows
            public void run() {
                print.printB();
            }

        }).start();


//        executorService.execute(new Runnable() {
//            @SneakyThrows
//            @Override
//            public void run() {
//                for (int i = 0; i < 10 ; i++){
//                    System.out.println("A");
//                    Thread.sleep(1000);
//                }
//            }
//        });
//        executorService.execute(new Runnable() {
//            @SneakyThrows
//            @Override
//            public void run() {
//                for (int i = 0; i < 10 ; i++){
//                    System.out.println("B");
//                    Thread.sleep(1000);
//                }
//            }
//        });


//        ReentrantLock

//        Node
//        List<Integer> arrayList = new ArrayList<>();
//
//        CompletableFuture.runAsync(() -> {
//                test();
//        });
//
//        for (int i = 0; i < 20; i++) {
//            arrayList.add(i);
//        }
//        Iterator<Integer> iterator = arrayList.iterator();
//        while (iterator.hasNext()){
//            Integer next = iterator.next();
//            if (next == 1){
//                iterator.remove();
//            }
//        }
//        for (int i = 0; i < arrayList.size(); i++){
//            if (arrayList.get(i) == 10){
//                arrayList.remove(arrayList.get(i));
//            }
//        }
//
//        for (Integer i : arrayList){
//            System.out.println(i);
//        }

//        Stream<Integer> streamOne = arrayList.stream().limit(5);
//
//        Stream<Integer> streamTwo = arrayList.stream().skip(14);

//        Stream<Integer> concat = Stream.concat(streamOne, streamTwo).distinct();

//        concat.forEach(System.out::println);
    }

    private static void test() {

    }

    /**
     * @param arr
     * @param key
     * @return
     */
    private static int binarySearch(int[] arr, int key) {
        int low = 0;
        int high = arr.length - 1;
        int middle = 0;            //定义middle

        if (key < arr[low] || key > arr[high] || low > high) {
            return -1;
        }

        while (low <= high) {
            middle = (low + high) / 2;
            if (arr[middle] > key) {
                //比关键字大则关键字在左区域
                high = middle - 1;
            } else if (arr[middle] < key) {
                //比关键字小则关键字在右区域
                low = middle + 1;
            } else {
                return middle;
            }
        }

        return -1;
    }
}
